<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

session_start();

/**
 * Clase Frontal de Inicio ( JL Framework )
 */
class JLFrontController {
    # Inicializa Framework

    static function main() {
        // Requerimos las clases necesarias
        include_once JLPATH_LIBRARIES . DS . 'Config.class.php'; //Clase configuraciones.
        include_once JLPATH_LIBRARIES . DS . 'SPDO.class.php'; //Clase DataBase Mysql con singleton
        include_once JLPATH_LIBRARIES . DS . 'DataBasePDO.class.php'; //Clase DataBase Mysql con singleton
        include_once JLPATH_LIBRARIES . DS . 'View.class.php'; //Mini motor de plantillas
        include_once JLPATH_LIBRARIES . DS . 'ControllerBase.class.php'; //Clase Controlador Base
        include_once JLPATH_LIBRARIES . DS . 'ModelBase.class.php'; //Clase Modelo Base
        include_once JLPATH_LIBRARIES . DS . 'Tools/JLTools.class.php'; //Clase Modelo Base
        include_once JLPATH_LIBRARIES . DS . 'JLHelper.class.php'; //Clase Modelo Base
        include_once JLPATH_SITE . DS . 'config.php'; //Clase Modelo Base

        $ws = trim(filter_var($_GET['ws'], FILTER_SANITIZE_STRING));
        $act = filter_var($_GET['act'], FILTER_SANITIZE_STRING);
        
        if (empty($ws)) {
            $ws = "Index";
        }
        
        if (empty($act)) {
            $act = "Index";
        }

        if ($_SESSION['_session_id'] != session_id()) {
            $conf = Config::getInstance();
            $allow_list = $conf->get('URL_MAP');
            if (!(!empty($ws) && array_key_exists($ws, $allow_list) && !empty($act) && in_array($act, $allow_list[$ws]))) {
                header("Location: ../");
                exit;
            }
        }

        $_type = $_SESSION['_int_id_typeuser'];

        if ($_type != 1) {
            $ctrl = $ws;
            switch ($ctrl) {
                case 'Presentation';
                case 'Administration';
                case 'ContactsCSME';
                case 'Newsletter';
                    if ($act != 'showNewsletter') {
                        $controllerName = "IndexController";
                        $actionName = "Index";
                    } else {
                        if (!empty($ws))
                            $controllerName = $ws . 'Controller';
                        if (!empty($act))
                            $actionName = $act;
                    }
                    break;
                default:
                    if (!empty($ws))
                        $controllerName = $ws . 'Controller';
                    if (!empty($act))
                        $actionName = $act;
                    break;
            }
        } else {
            if (!empty($ws))
                $controllerName = $ws . 'Controller';
            if (!empty($act))
                $actionName = $act;
        }

        $controllerPath = JLPATH_CONTROLLERS . DS . $controllerName . '.php';

        if (!is_file($controllerPath)) {
            $controllerName = "IndexController";
            $controllerPath = JLPATH_CONTROLLERS . DS . $controllerName . '.php';
        }

        require $controllerPath;

        if (is_callable(array($controllerName, $actionName)) == false) {
            $actionName = "Index";
        }

        $controller = new $controllerName();
        $controller->$actionName();
    }

}